package Servlet;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/gobangServlet"})
public class gobangServlet extends HttpServlet {
    public static int[] nowcolor=new int[10000];
    public static int[] bzx=new int[10000];
    public static int[] hzx=new int[10000];
    public static int[] bzy=new int[10000];
    public static int[] hzy=new int[10000];
    public static int[] tc=new int[10000];
    public static void minit(int hm)
    {
        nowcolor[hm]=1;
        bzx[hm]=-1;
        hzx[hm]=-1;
        bzy[hm]=-1;
        hzy[hm]=-1;
        tc[hm]=-1;
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int dg=Integer.valueOf(request.getParameter("dg"));//1为下子，0为获取;
        int color=Integer.valueOf(request.getParameter("color"));
        int isWin=Integer.valueOf(request.getParameter("isWin"));
        int istc=Integer.valueOf(request.getParameter("tc"));
        int hm=Integer.valueOf(request.getParameter("hm"));
        System.out.println(hm);
        if(istc==1)
        {
            tc[hm]=1;
        }
        if(isWin==1)
        {
            minit(hm);
            return;
        }
        if(dg==0)
        {
            if(color%2==1)//1是黑子
            {
                out.print(bzx[hm]+" "+bzy[hm]+" "+tc[hm]);
            }
            else
            {
                out.print(hzx[hm]+" "+hzy[hm]+" "+tc[hm]);
            }
            return;
        }
        int x=Integer.valueOf(request.getParameter("x"));
        int y=Integer.valueOf(request.getParameter("y"));
        if(color!=nowcolor[hm])return;

        if(nowcolor[hm]==1)
        {
            hzx[hm]=x;
            hzy[hm]=y;
        }
        else {
            bzx[hm]=x;
            bzy[hm]=y;
        }
        nowcolor[hm]++;
        nowcolor[hm]%=2;
    }
}
